#include "CSimpleList.h"

CSimpleList::CSimpleList(int nNextOffset)
{
    m_pHead = NULL; 
    m_nNextOffset = nNextOffset;
}

void CSimpleList::Construct(int nNextOffset)
{
    m_nNextOffset = nNextOffset;
}

BOOL CSimpleList::IsEmpty() const
{
    return m_pHead == NULL;
}

void CSimpleList::AddHead(void* p)
{
    *GetNextPtr(p) = m_pHead;
    m_pHead = p;
}

void CSimpleList::RemoveAll()
{
    m_pHead = NULL;
}

void* CSimpleList::GetHead() const
{
    return m_pHead;
}

void* CSimpleList::GetNext(void* p) const
{
    if (!p) return NULL;
    return *GetNextPtr(p);
}

BOOL CSimpleList::Remove(void* p)
{
    if (p == NULL)
        return FALSE;

    BOOL bRet = FALSE;
    if (p == m_pHead) 
    {
        m_pHead = *GetNextPtr(p);
        bRet = TRUE;
    }
    else 
    {
        void* pNode = m_pHead;

        while (pNode != NULL && *GetNextPtr(pNode) != p) 
        {
            pNode = *GetNextPtr(pNode);
        }

        if (pNode != NULL) 
        {
            *GetNextPtr(pNode) = *GetNextPtr(p);
            bRet = TRUE;
        }
    }
    return bRet;
}

void** CSimpleList::GetNextPtr(void* p) const
{
    return (void**)((BYTE*)p + m_nNextOffset);
}
